package com.edu.zut.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.zut.entity.Role;
import com.edu.zut.entity.User;
import com.edu.zut.mapper.RoleMapper;
import com.edu.zut.mapper.UserMapper;
import com.edu.zut.result.HttpCode;
import com.edu.zut.result.ResponseResult;
import com.edu.zut.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 许文科
 * @since 2023-01-12
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserMapper userMapper;
    QueryWrapper<Role> wrapper = new QueryWrapper<>();

    @Override
    public ResponseResult getRoles(String username) {
        //查出userId
        Long userId = userMapper.getUserIdByUserName(username);
//        List<Role> roles = roleMapper.getRolesByUserId(userId);
        List<Role> roles = roleMapper.selectList(null);
        if (CollectionUtils.isEmpty(roles)) {
            return new ResponseResult("400", "失败", null);
        }

        return new ResponseResult(HttpCode.SUCCESS, "成功", roles);
    }

    @Override
    public ResponseResult updateStatus(Long roleId, String status) {

        Role role = roleMapper.selectOne(wrapper.eq("role_id", roleId));

        role.setStatus(status);
        int flag = roleMapper.updateById(role);
        if (flag == 1) {
            return new ResponseResult(HttpCode.SUCCESS, "修改成功", null);
        }
        return new ResponseResult(HttpCode.ERROR, "修改失败", null);
    }
}
